package org.jeecg.modules.rental.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.rental.vo.LoginRentalAccount;
import org.jeecg.modules.iot.api.IControlAPI;
import org.jeecg.modules.iot.cache.EBikeCache;
import org.jeecg.modules.rental.entity.RentalAccountPackage;
import org.jeecg.modules.rental.entity.RentalEBike;
import org.jeecg.modules.rental.entity.RentalEbikeReturn;
import org.jeecg.modules.rental.entity.RentalOrder;
import org.jeecg.modules.rental.enums.DepositAuditStatusEnums;
import org.jeecg.modules.rental.enums.DepositStatusEnums;
import org.jeecg.modules.rental.enums.EBikeOrderTypeEnums;
import org.jeecg.modules.rental.mapper.RentalAccountPackageMapper;
import org.jeecg.modules.rental.mapper.RentalEBikeMapper;
import org.jeecg.modules.rental.mapper.RentalEbikeReturnMapper;
import org.jeecg.modules.rental.mapper.RentalOrderMapper;
import org.jeecg.modules.rental.service.IRentalEbikeReturnService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @Description: 还车记录
 * @Author: sean
 * @Date:   2024-12-14
 * @Version: V1.0
 */
@Service
@RequiredArgsConstructor
public class RentalEbikeReturnServiceImpl extends ServiceImpl<RentalEbikeReturnMapper, RentalEbikeReturn> implements IRentalEbikeReturnService {

    private final RentalAccountPackageMapper rentalAccountPackageMapper;
    private final RentalOrderMapper rentalOrderMapper;
    private final RentalEBikeMapper rentalEBikeMapper;
    private final EBikeCache eBikeCache;
    private final IControlAPI iControlAPI;

    @Override
    public IPage<RentalEbikeReturn> selectJoinPage(Page<RentalEbikeReturn> page, Wrapper wrapper) {
        IPage<RentalEbikeReturn> pageList = baseMapper.selectJoinPage(page, wrapper);
        if(ObjectUtil.isNotEmpty(pageList) && ObjectUtil.isNotEmpty(pageList.getRecords())){
            pageList.getRecords().stream().forEach(item->{
                DepositStatusEnums depositStatusEnums = DepositStatusEnums.getEnumByValue(item.getDepositStatus().toString());
                if(ObjectUtil.isNotEmpty(depositStatusEnums)){
                    item.setDepositStatusTxt(depositStatusEnums.getLabel().toString());
                }
                DepositAuditStatusEnums auditStatusEnums = DepositAuditStatusEnums.getEnumByValue(item.getReviewStatus().toString());
                if(ObjectUtil.isNotEmpty(auditStatusEnums)){
                    item.setReviewStatusTxt(auditStatusEnums.getLabel().toString());
                }
            });
        }
        return pageList;
    }

    @Override
    public Boolean ebikeReturn(Long accountPackageId) {
        LoginRentalAccount rentalAccount = (LoginRentalAccount) SecurityUtils.getSubject().getPrincipal();
        RentalAccountPackage rentalAccountPackage = rentalAccountPackageMapper.selectById(accountPackageId);
        if(!rentalAccount.getId().toString().equals(rentalAccountPackage.getAccountId())){ //判断是不是我的套餐
            throw new JeecgBootException("不是您的套餐，您不可以还车");
        }
        RentalEBike car= rentalEBikeMapper.selectById(rentalAccountPackage.getCarId());
        System.out.println("还车开始——————————————————————————====="+car.getId());
        if ("2".equals(rentalAccountPackage.getPkgCategory())){
            rentalAccountPackage.setPkgStatus("1");
            rentalAccountPackage.setCarId(null);
        }else {
            RentalOrder rentalOrder = rentalOrderMapper
                    .selectOne(new QueryWrapper<RentalOrder>()
                            .in("order_type", new String[]{EBikeOrderTypeEnums.RENTAL_ORDER.getValue(), EBikeOrderTypeEnums.PREBOOK_ORDER.getValue()} )
                            .eq("account_package_id", rentalAccountPackage.getId()));
            /*加还车记录*/
            RentalEbikeReturn rcr = new RentalEbikeReturn();
            rcr.setAccountPackageId(accountPackageId);
            rcr.setOrderId(rentalOrder.getId());
            rcr.setAccountId(rentalAccount.getId().toString());
            rcr.setCarId(rentalAccountPackage.getCarId());
            rcr.setStartTime(rentalAccountPackage.getStartTime());
            rcr.setEndTime(LocalDateTime.now());
            rcr.setDepositAmount(rentalOrder.getDepositAmount());
            if(rentalOrder.getDepositAmount().compareTo(BigDecimal.ZERO) > 0){
                rcr.setDepositStatus(0);
                rcr.setReviewStatus(1);
            }else{
                rcr.setDepositStatus(2);
                rcr.setReviewStatus(2);
            }
            rcr.setCreateTime(LocalDateTime.now());
            rcr.setSysOrgCode(rentalAccountPackage.getSysOrgCode());
            baseMapper.insert(rcr);
            rentalAccountPackage.setDepositStatus(2);
            rentalAccountPackage.setPkgStatus("2");
            rentalAccountPackage.setReturnTime(LocalDateTime.now());
        }
        if (car!=null){
            eBikeCache.setVehicleStatus(car.getCarCode(),"1");
            iControlAPI.returnCar(car.getCarCode(),car.getPhone());
        }
        return rentalAccountPackageMapper.updateById(rentalAccountPackage) > 0;
    }

    @Override
    public Boolean refundDeposit(Long accountPackageId) {
        LoginRentalAccount rentalAccount = (LoginRentalAccount) SecurityUtils.getSubject().getPrincipal();
        RentalAccountPackage rentalAccountPackage = rentalAccountPackageMapper.selectById(accountPackageId);
        if (rentalAccountPackage.getCarId() != null){
            throw new JeecgBootException("revert.service.car.not.returned");
        }
        RentalOrder rentalOrder = rentalOrderMapper
                .selectOne(new QueryWrapper<RentalOrder>()
                        .eq("account_package_id", rentalAccountPackage.getId()));
        RentalEbikeReturn rcr = new RentalEbikeReturn();
        rcr.setAccountPackageId(accountPackageId);
        rcr.setOrderId(rentalOrder.getId());
        rcr.setAccountId(rentalAccount.getId().toString());
        rcr.setCarId(rentalAccountPackage.getCarId());
        rcr.setStartTime(rentalAccountPackage.getStartTime());
        rcr.setEndTime(LocalDateTime.now());
        rcr.setDepositAmount(rentalOrder.getDepositAmount());
        if(rentalOrder.getDepositAmount().compareTo(BigDecimal.ZERO) > 0){
            rcr.setDepositStatus(0);
            rcr.setReviewStatus(1);
        }else{
            rcr.setDepositStatus(2);
            rcr.setReviewStatus(2);
        }
        rcr.setCreateTime(LocalDateTime.now());
        rcr.setSysOrgCode(rentalAccountPackage.getSysOrgCode());
        baseMapper.insert(rcr);
        rentalAccountPackage.setDepositStatus(2);
        return rentalAccountPackageMapper.updateById(rentalAccountPackage) > 0;
    }


}
